<?php
namespace app\admin\controller;
use think\Db;
class Goodscat extends Base
{
    //列表
    public function lists()
    {
        if(is_post()){
            $cur_page     = input('page');
            $keyword  = input('keyword');
            $status  = input('status');
            $parent_id  = input('parent_id');
            $min_time = input('min_time');
            $max_time = input('max_time');
            $sql = "
                select
                    [1]
                from
                    goods_cat a
                left join
                    goods_cat b
                on
                    a.parent_id = b.id
                ";
            $param = [];
            $w = ['where'];
            if(!empty($min_time)){
                $w[] = ' and a.create_time>?';
                $param[] = strtotime($min_time);
            }
            if(!empty($max_time)){
                $w[] = ' and a.create_time<?';
                $param[] = strtotime($max_time);
            }
            if(!empty($keyword)){
                $w[] = ' and a.cat_title like ?';
                $param[] = "%{$keyword}%";
            }
            if($status!='100all'){
                $w[] = ' and a.status = ?';
                $param[] = $status;
            }
            if($parent_id!='100all'){
                $w[] = ' and a.parent_id = ?';
                $param[] = $parent_id;
            }
            #条件判断
            if (count($w)>1){
              $w[1] = str_replace("and","",$w[1]);
              $sql .= implode(" ",$w);
            }
            #分页
            $page_size = c('a.page_size');
            $page_html = com_list_page($sql, $param, $cur_page,$page_size);

            #内容
            $start = ($cur_page - 1) * $page_size;
            $field = "a.*,b.cat_title as parent_title";
            $sql_list = str_replace("[1]",$field,$sql);
            $sql_list .= " order by a.sort desc, a.id desc limit {$start},{$page_size}";
            $rslist = Db::query($sql_list, $param);
            foreach ($rslist as $k => $v) {
                $rslist[$k]['create_time'] = com_formate_time($v['create_time']);
                $rslist[$k]['checkbox'] = com_checkbox($v['id']);
                $rslist[$k]['sort']   = com_sort('goods_cat',$v['id'],$v['sort']);
                $rslist[$k]['status'] = c('type.status',$v['status']);
                $rslist[$k]['action']  = com_btn_edit("/goodscat/edit/id/{$v['id']}","800px","400px");
                if($v['parent_id']!=0){
                  $rslist[$k]['action']  .= com_btn_edit("/goodscatattr/lists/id/{$v['id']}",'','','编辑属性');
                }
                $rslist[$k]['action']  .= com_btn_del("/goodscat/delete/id/{$v['id']}", "您确定要删除【{$v['cat_title']}】？");
            }
            $body_html = com_list_body($rslist, ["checkbox","cat_title","parent_title","sort", "status", "create_time", "action"]);
            com_check(['data'=>['com_body'=>$body_html, 'com_page'=>$page_html]]);
        }
        $all= [
            '100all' => [
                'id' => '100all',
                'parent_id' => 0,
                'cat_title' => '全部分类',
                'depath' => 0,
                ]
        ];
        $catlist = $all + \app\admin\model\GoodsCat::allSubCat();
        return view('',['status'=>c('type.status'),'catlist'=>$catlist]);
    }
    #添加
    function add(){
      if(is_post()){
            $d = input();
            $rule = [
                'cat_title'=>  'require',
            ];
            $msg = [
              'cat_title.require'   =>  '请输入标题',
            ];
            com_validate($rule, $msg, $d);
            $d['cat_tree'] = \app\admin\model\GoodsCat::buildTree($d['parent_id']);
            $d['create_time'] = time();
            $d['update_time'] = time();
            Db::name("goods_cat")->insert($d);
            com_check(['suc_msg'=>'添加成功!','suc_js'=>'alert|close|reload']);
      }

      $all= [
          '100all' => [
              'id' => 0,
              'parent_id' => 0,
              'cat_title' => '顶级分类',
              'depath' => 0,
              ]
      ];
      $catlist = $all + \app\admin\model\GoodsCat::allSubCat();
      return view('',['catlist'=>$catlist]);
    }
    #编辑
    function edit($id){
      if(is_post()){
            $d = input();
            $rule = [
                'id'=>  'require',
                'cat_title'=>  'require',
                'status'  =>  'require',
            ];
            $msg = [
              'id.require'   =>  '请输入id',
              'cat_title.require'   =>  '请输入标题',
              'status.require'   =>  '请选择状态',
            ];
            com_validate($rule, $msg, $d);
            $d['cat_tree'] = \app\admin\model\GoodsCat::buildTree($d['parent_id']);
            $d['update_time'] = time();
            Db::name("goods_cat")->update($d);
            com_check(['suc_msg'=>'修改成功!','suc_js'=>'alert|close|reload']);
      }

      $all= [
          '100all' => [
              'id' => 0,
              'parent_id' => 0,
              'cat_title' => '顶级分类',
              'depath' => 0,
              ]
      ];
      $catlist = $all + \app\admin\model\GoodsCat::allSubCat();
      $info = Db::name('goods_cat')->find($id);
      return view('',['info'=>$info,'status'=>c('type.status'),'catlist'=>$catlist]);
    }
    #删除
    function delete($id){
      $g  =  new \app\admin\model\GoodsCat();
      if( $g->checkDel( $id ) ){
          Db::name('goods_cat')->delete($id);
      }
      com_check(['suc_msg'=>'删除成功!','suc_js'=>'alert|close|reload']);
    }
    #批量删除
    function delall(){
      if(is_post()){
          $ids = input('ids/a');
          $g  =  new \app\admin\model\GoodsCat();
          foreach($ids as $id){
              if( $g->checkDel($id) ){
                     Db::name('goods_cat')->delete($id);
              }
          }
          com_check(['suc_msg'=>'删除成功!','suc_js'=>'alert|close|reload']);
      }
    }
}
